package com.ease.actionscript.mapping.mapper;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class AbstractValueObjectClassMapper extends AbstractActionScriptClassMapper {

	public boolean isCompliant() {
		return super.isCompliant();
	}

	public void prepare() {
		super.prepare();

		/** */
		Class<?> clazzSuperclass = clazz.getSuperclass();
		if (null != clazzSuperclass) {
			if ((!clazzSuperclass.equals(Object.class)) && //
					(!clazzSuperclass.equals(Enum.class)) && //
					(Serializable.class.isAssignableFrom(clazzSuperclass))) {
				this.superclass = clazzSuperclass;
				this.addImportClassName(this.getActionScriptTypeName(superclass));
			}
		}

		// /** */
		// actionScriptMappingContext.addIncludeClass(clazz, this.projectName);

		/** */
		Class<?>[] interfaces = clazz.getInterfaces();
		if (null != interfaces) {
			for (Class<?> interfaceClass : interfaces) {
				if (packageNameStartsWith(interfaceClass.getPackage().getName(), actionScriptMappingContext.getPackagePrefixs()) && //
						!interfaceClass.getName().endsWith("Util") && //
						!interfaceClass.getName().endsWith("Const")) {
					String interfaceName = interfaceClass.getName();
					this.addInterfaceName(interfaceName);
					this.addImportClassName(interfaceName);
				}
			}
		}

		/** */
		// TODO BeanUtils.getPropertyDescriptors(clazzSuperclass);//
		Field[] fields = clazz.getDeclaredFields();

		// if (null != superclass &&
		// superclass.getPackage().getName().startsWith(PACKAGE_PREFIX)) {
		// fields = clazz.getDeclaredFields();
		// } else {
		// // fields = clazz.getFields();
		// fields = clazz.getDeclaredFields();
		// }

		/**
		 * BeanInfo beanInfo = Introspector.getBeanInfo(clazz); // PropertyDescriptor[] props =
		 * beanInfo.getPropertyDescriptors(); // Introspector.decapitalize
		 */

		if (null != fields) {
			for (Field field : fields) {
				String fieldTypeName = null;
				if (!"serialVersionUID".equals(field.getName()) && //
						!"ENUM$VALUES".equals(field.getName())// java.lang.ENUM
				) {
					if (this.isRemotingMapped(field)) {

						fieldTypeName = this.getActionScriptTypeName(field.getType());
						if (null != fieldTypeName && fieldTypeName.indexOf('.') > 0) {
							this.addImportClassName(fieldTypeName);
						}

						if (Modifier.isStatic(field.getModifiers())) {
							if (Modifier.isPublic(field.getModifiers())) {
								this.addStaticField(field);
							}
						} else {
							this.addMemberField(field);
						}
					}

				}
			}
		}

	}

}
